package com.shengqi.xinnongren.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.ui.goods.adapter.OrderGoodsAdapter;
import com.shengqi.xinnongren.work.ui.goods.adapter.PicturesAdapter;
import com.shengqi.xinnongren.work.ui.home.model.CartGoodsBean;
import com.shengqi.xinnongren.work.ui.user.model.RefundDetailsBean;
import com.shengqi.xinnongren.work.ui.user.presenter.RefundDetailsPresenter;
import com.shengqi.xinnongren.work.ui.user.view.RefundDetailsView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 退款详情
 */
public class RefundDetailsActivity extends BaseMvpActivity<RefundDetailsView, RefundDetailsPresenter> implements RefundDetailsView {

    private static final String EXTRA_ID = "order_id";
    @BindView(R.id.rv_progress)
    RecyclerView mRecyclerView;

    @BindView(R.id.tv_refund_money)
    TextView mTvRefundMoney;
    @BindView(R.id.tv_refund_type)
    TextView mTvRefundType;
    @BindView(R.id.tv_remark)
    TextView mTvRemark;
    @BindView(R.id.tv_refund_time)
    TextView mTvRefundTime;
    @BindView(R.id.rv_order_goods)
    RecyclerView mRvOrderGoods;
    @BindView(R.id.rv_pictures)
    RecyclerView mRvPictures;

    private String mId;

    private ProgressAdapter mAdapter;
    private List<RefundDetailsBean.ReturnInfoBean> mList;
    private OrderGoodsAdapter mOrderGoodsAdapter;
    private List<CartGoodsBean> mCartGoodsList = new ArrayList<>();

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, RefundDetailsActivity.class);
        intent.putExtra(EXTRA_ID, id);
        context.startActivity(intent);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_refund_details;
    }

    @Override
    protected void initView() {

        Intent intent = getIntent();
        if (intent != null) {
            mId = intent.getStringExtra(EXTRA_ID);
        }

        mList = new ArrayList<>();
        mAdapter = new ProgressAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        mOrderGoodsAdapter = new OrderGoodsAdapter(this, mCartGoodsList);
        mRvOrderGoods.setAdapter(mOrderGoodsAdapter);
        mRvOrderGoods.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.getData(mId);
    }

    @Override
    protected RefundDetailsPresenter createPresenter() {
        return new RefundDetailsPresenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
    }

    @Override
    public void showRequestFailed(String message) {
        ToastHelper.getInstance(this).showWarn(message);
    }

    @Override
    public void showData(RefundDetailsBean bean) {
        mTvRefundMoney.setText(String.format("%s元", bean.getAmount()));
        mTvRefundType.setText(bean.getPayStatus());
        mTvRefundTime.setText(bean.getLastDay());
        mTvRemark.setText(bean.getRemarks());

        mList.clear();
        mList.addAll(bean.getReturnInfo());
        mAdapter.notifyDataSetChanged();

        if (bean.getCommodityList() != null)
            mCartGoodsList.addAll(bean.getCommodityList());
        mOrderGoodsAdapter.notifyDataSetChanged();

        String picStr = bean.getPic();
        if (!TextUtils.isEmpty(picStr)) {
            String[] pics = picStr.split(",");
            if (pics.length == 0) return;
            mRvPictures.setAdapter(new PicturesAdapter(this, PicturesAdapter.Type.GRID, pics));
            mRvPictures.setLayoutManager(new GridLayoutManager(this, 3));
        }
    }

    class ProgressAdapter extends RecyclerView.Adapter<ProgressAdapter.Holder> {

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new Holder(getLayoutInflater().inflate(R.layout.item_refund_progress, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int position) {
            RefundDetailsBean.ReturnInfoBean bean = mList.get(position);
            holder.tvTitle.setText(bean.getTitle());
            holder.tvContent.setText(bean.getContent());
            holder.tvTime.setText(bean.getTime());

            if (position == 0)
                setIsEnableView(holder, true);
            else
                setIsEnableView(holder, false);
        }

        private void setIsEnableView(Holder holder, boolean isEnable) {
            holder.tvDot.setEnabled(isEnable);
            holder.tvTitle.setEnabled(isEnable);
            holder.tvContent.setEnabled(isEnable);
            holder.tvTime.setEnabled(isEnable);
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        public class Holder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_dot)
            TextView tvDot;
            @BindView(R.id.tv_title)
            TextView tvTitle;
            @BindView(R.id.tv_content)
            TextView tvContent;
            @BindView(R.id.tv_time)
            TextView tvTime;

            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
